﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using BOMS.Utility.DATA;
using AjaxPro;
using System.Web.UI.HtmlControls;
namespace BOMS.WebUI.Web.PowerControl
{
    public partial class PowerDownState : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Repeater1.ItemDataBound +=new RepeaterItemEventHandler(Repeater1_ItemDataBound);
            if (!Page.IsPostBack)
            {
                BindRepeater();
                AjaxPro.Utility.RegisterTypeForAjax(this.GetType(), this.Page);
            }
        }
        [AjaxPro.AjaxMethod]
        public string TimeCalculate(decimal GrpCapcity, decimal CurI, DateTime PowerDownTime)
        {
            decimal restSec = (GrpCapcity/CurI) * 3600;//剩余秒
            TimeSpan ts1 = new TimeSpan(0,0,(int)Math.Round(restSec));
            TimeSpan ts2 = ts1 - (DateTime.Now - PowerDownTime);
            if (ts2 < new TimeSpan(0, 0, 0))
            {
                ts2 = new TimeSpan(0, 0, 0);
            }
            string str = string.Format("{0}:{1:00}",ts2.Hours,ts2.Minutes);
            return str;
        }

        void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
                e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Model.PowerControl model = e.Item.DataItem as Model.PowerControl;
                if (model != null)
                {
                    Literal ltrResDscTime = e.Item.FindControl("ltrResDscTime") as Literal;
                    HtmlAnchor lnkView = e.Item.FindControl("lnkView") as HtmlAnchor;
                    if (!string.IsNullOrEmpty( model.IsDealwith) && model.IsDealwith.Trim() == "已处理")
                    {
                        ltrResDscTime.Text = model.ResDscTime;
                        lnkView.Visible = true;
                        lnkView.Attributes["onclick"] = "javascript:Open('" + model.ID + "');";
                    }
                    else
                    {
                        ltrResDscTime.Text = TimeCalculate(model.BatGrpCapFree.Value, model.BatGrpDscI.Value, model.PowerDownTime.Value);
                        lnkView.Visible = false;
                    }
                }
            }
        }

        void BindRepeater()
        {
            BLL.PowerControl bll = new BLL.PowerControl();
            List<Model.PowerControl> list = bll.GetModelList("BatGrpDscV=0");
            Repeater1.DataSource = list;
            Repeater1.DataBind();
        }
    }
}